﻿#region 命名空间

using System;
using System.Web;
using System.Web.UI.WebControls;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Model.Attribute;
using WinnerCMS.Model.Enum;
using WinnerCMS.WebFactory;

#endregion

namespace WinnerCMS.WebUI.Admin.Special
{
    [Access("273002,273004")]
    public partial class Add : PageBase
    {
        private readonly SpecialManage MyBLL = new SpecialManage();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var action = Request["Action"];
                if (action == "Edit")
                {
                    EditUiLoad();
                }
                else
                {
                    AddUiLoad();
                }
            }
        }

        private void AddUiLoad()
        {
            Title = "添加栏目";
            this.btnOK.Text = "添加";

            this.MyBLL.BindDDL(this.ddlSpecial, BindOption.NoLink, "　　　", null);
            this.MyBLL.BindDDL(this.ddlCopySpecial, BindOption.NoLink, "<-无->", null);
            new ModelManage().BindDDL(this.ddlModel, "<请选择模型>");

            if (Request["ParentId"] != null)
            {
                this.ddlSpecial.SelectedValue = Request["ParentId"];
            }
        }

        private void EditUiLoad()
        {
            Title = "修改栏目";
            this.btnOK.Text = "修改";


            this.MyBLL.BindDDL(this.ddlSpecial, BindOption.NoLink, "　　　", null);
            this.MyBLL.BindDDL(this.ddlCopySpecial, BindOption.NoLink, "<-无->", null);
            new ModelManage().BindDDL(this.ddlModel, "<请选择模型>");

            var model = this.MyBLL.GetInfo(int.Parse(Request["ID"]));
            this.ddlSpecial.SelectedValue = model.ParentId.ToString();
            this.ddlSpecial.Enabled = false;
            this.txtSpecialName.Text = model.SpecialName;
            this.txtSpecialName_EN.Text = model.SpecialNameEN;
            this.txtTemplateFileName.Text = model.TemplateFileName;
            this.txtLinkUrl.Text = model.LinkUrl;
            this.UC_AdminSelect1.lblSelected.Text = model.ContentAdmin;

            this.UC_AdminSelect2.lblSelected.Text = model.SpecialAdmin;

            this.txtSpecialFilePostfix.Text = model.SpecialFilePostfix;
            this.txtSpecialFilePrefix.Text = model.SpecialFilePrefix;
            this.RBL_SpecialURLType.SelectedValue = model.SpecialURLStyle.ToString();
            this.txtSpecialIntro.Text = model.SpecialIntro;
            this.txtSpecialPic.Text = model.SpecialPic;
            this.txtSpecialDescription.Text = model.SpecialDescription;
            this.txtSpecialKeyWord.Text = model.SpecialKeyWord;
            this.RBL_OpenGrade.SelectedValue = model.OpenGrade.ToString();


            this.CBL_Fields.DataTextField = "Title";
            this.CBL_Fields.DataValueField = "FieldName";
            this.CBL_Fields.DataSource = new FieldManage().GetShopModelField(byte.Parse(this.ddlModel.SelectedValue));
            this.CBL_Fields.DataBind();

            //加载被勾选的字段
            var list = new GoodsCustomFieldsManage().GetFieldsList(int.Parse(Request["ID"]));
            foreach (var g in list)
            {
                foreach (ListItem li in this.CBL_Fields.Items)
                {
                    if (li.Value == g.FieldName)
                        li.Selected = true;
                }
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            WinnerCMS.Model.Special model;

            if (Request["ID"] == null)
            {
                model = new WinnerCMS.Model.Special();
                model.Id = 0;
                model.ParentId = int.Parse(this.ddlSpecial.SelectedValue);
            }
            else
            {
                model = this.MyBLL.GetInfo(Convert.ToInt32(Request["ID"]));
            }

            model.SpecialName = this.txtSpecialName.Text.Trim();
            model.SpecialNameEN = this.txtSpecialName_EN.Text.Trim();
            model.TemplateFileName = this.txtTemplateFileName.Text.Trim();
            model.LinkUrl = this.txtLinkUrl.Text.Trim();
            model.ContentAdmin = this.UC_AdminSelect1.lblSelected.Text.Trim();
            model.SpecialAdmin = this.UC_AdminSelect2.lblSelected.Text.Trim();
            model.SpecialFilePostfix = this.txtSpecialFilePostfix.Text.Trim();
            model.SpecialFilePrefix = this.txtSpecialFilePrefix.Text.Trim();
            model.SpecialURLStyle = int.Parse(this.RBL_SpecialURLType.SelectedValue);
            model.SpecialIntro = this.txtSpecialIntro.Text.Trim();
            model.SpecialPic = this.txtSpecialPic.Text.Trim();
            model.SpecialDescription = this.txtSpecialDescription.Text.Trim();
            model.SpecialKeyWord = this.txtSpecialKeyWord.Text.Trim();
            model.OpenGrade = int.Parse(this.RBL_OpenGrade.SelectedValue);


            var resu = this.MyBLL.Save(model);

            if (string.IsNullOrEmpty(resu.Errors) == false)
            {
                MsgBox.Message(resu.Errors, this);
            }
            else
            {
                HttpContext.Current.Response.Redirect("Manage.aspx");
            }
        }

        protected void ddlCopySpecial_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        protected void ddlSpecial_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.CBL_Fields.DataSource = new FieldManage().GetShopModelField(byte.Parse(this.ddlModel.SelectedValue));
            this.CBL_Fields.DataTextField = "Title";
            this.CBL_Fields.DataValueField = "FieldName";
            this.CBL_Fields.DataBind();
        }
    }
}